package edu.java.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Dispatch;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import edu.java.ishop.dao.ProductDao;
import edu.java.ishop.dao.impl.ProductDatabasaDao;
import edu.java.ishop.dao.impl.ProductFileDao;
import edu.java.ishop.dao.impl.ProductSocketDao;
import edu.java.ishop.model.Product;
import edu.java.ishop.model.ProductCategory;
import edu.java.ishop.services.ProductService;
import edu.java.ishop.services.ProductServiceFactory;

public class MainServlet2 extends HttpServlet {

	private ProductService productService;
	
	@Override
	public void init() throws ServletException {
//		productService = new ProductService();
//		//ProductDao dao = new ProductSocketDao();
//		ProductDao dao = new ProductDatabasaDao();
//		productService.setDao(dao);
//		//ProductFileDao fileDao = new ProductFileDao();
//		//productService.setDao(fileDao);
		productService = WebApplicationContextUtils.getWebApplicationContext(
				getServletContext()).getBean(ProductService.class);
	}
    @Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Product> products = productService.getAll();
//		List<Product> products = new ArrayList<Product>();
//		Product p = new Product(1l);
//		p.setName("Mercedes");
//		p.setDescription("German car");
//		p.setPrice(new BigDecimal(100000));
//		ProductCategory pc = new ProductCategory(1l);
//		p.setCategory(pc);
//		products.add(p);
		
//		req.setAttribute("products", products);
//		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/products.jsp");
//		dispatcher.forward(req, resp);
		
//		

		
		List<ProductCategory> categories = productService.getCategories();
		request.setAttribute("products", products);
		request.setAttribute("categories", categories);
		RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/products.jsp");
		requestDispatcher.forward(request, response);
        
		//req.getSession().setAttribute("products", products);
		//resp.sendRedirect("products.jsp");
	}
    @Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
//		Product product = new Product(3l);
//		product.setName(req.getParameter("productName"));
//	    product.setPrice(BigDecimal.valueOf(Double.valueOf(req.getParameter("productPrice"))));
//	    //product.setCategory(new ProductCategory(Long.valueOf(req.getParameter("productCategoryId"))));
//	    product.setDescription(req.getParameter("productDescription"));
//	    ProductCategory category = new ProductCategory(2l);
//	    product.setCategory(category);
//	    productService.save(product);
//	    resp.sendRedirect("main2");
//	    //resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    	String path = request.getPathInfo();
		switch (path) {
		case "/add":
			Product p = new Product();
			p.setName(request.getParameter("productName"));
			p.setDescription(request.getParameter("productDescription"));
			p.setPrice(new BigDecimal((String) request
					.getParameter("productPrice")));
			p.setCategory(new ProductCategory(Long.valueOf(request
					.getParameter("categoryId")), ""));
			productService.saveProduct(p);
			break;
		case "/remove":
			final long productid = Long.valueOf(request.getParameter("productid"));
			productService.deleteProduct(productid);
			break;
		default:

		}
	}
    
    
}
